package com.media.selector.widget

import android.content.Context
import android.text.format.DateUtils
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.NonNull
import androidx.annotation.Nullable
import androidx.appcompat.widget.AppCompatImageView
import androidx.constraintlayout.widget.ConstraintLayout
import com.media.selector.MediaSelector
import com.media.selector.R
import com.media.selector.media.listener.OnMediaViewClickListener
import com.media.selector.media.model.entity.MediaEntity

/**
 * date        ：2020/5/12
 * author      ：蒙景博
 * description ：
 */
class MediaGridView: ConstraintLayout, View.OnClickListener {

    private lateinit var mThumbnail: AppCompatImageView
    private lateinit var mCheckView: AppCompatImageView
    private lateinit var mGifMask: AppCompatImageView
    private lateinit var mVideoDuration: TextView
    private lateinit var mVideoDurationContainer: LinearLayout

    private var mCurrentViewPosition = UNKNOWN_POSITION

    private var mMedia: MediaEntity? = null
    private var mOnMediaClickListener: OnMediaViewClickListener? = null

    companion object {
        // 未知 position
        const val UNKNOWN_POSITION = -1
    }

    constructor(@NonNull context: Context): this(context, null)
    constructor(@NonNull context: Context, @Nullable attrs: AttributeSet?): this(context, attrs, 0)
    constructor(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr) {
        initAttribute()
    }

    private fun initAttribute() {
        inflate(context, R.layout.layout_media_grid_container, this)
        mThumbnail = findViewById(R.id.media_thumbnail)
        mCheckView = findViewById(R.id.media_check)
        mGifMask = findViewById(R.id.media_gif_mask)
        mVideoDuration = findViewById(R.id.media_video_duration)
        mVideoDurationContainer = findViewById(R.id.media_video_duration_container)
        mThumbnail.setOnClickListener(this)
        mCheckView.setOnClickListener(this)
    }

    fun setMediaData(listener: OnMediaViewClickListener, position: Int, media: MediaEntity): MediaGridView {
        this.mOnMediaClickListener = listener
        this.mCurrentViewPosition = position
        this.mMedia = media
        setMediaView(media)
        return this
    }

    override fun onClick(view: View?) {
        if (view?.id == R.id.media_thumbnail) {
            mMedia?.let { mOnMediaClickListener?.onThumbnailClicked(mCurrentViewPosition, it) }
        } else if (view?.id == R.id.media_check) {
            mMedia?.let { mOnMediaClickListener?.onCheckViewClicked(mCurrentViewPosition, it) }
        }
    }

    fun setChecked(checked: Boolean): MediaGridView {
        mCheckView.isSelected = checked
        val mCheckedResource = if (checked) R.drawable.svg_media_check_sel else R.drawable.svg_media_check_nor
        mCheckView.setImageResource(mCheckedResource)
        return this
    }

    private fun setMediaView(media: MediaEntity) {
        MediaSelector.with().load(media.contentUri)?.thumbnail(mThumbnail)
        // 设置GIF标识
        mGifMask.visibility = if (media.isGif()) View.VISIBLE else View.GONE
        // 设置视频时间
        mVideoDurationContainer.visibility = if (media.isVideo()) View.VISIBLE else View.GONE
        mVideoDuration.text = mMedia?.duration?.div(1000)?.let { DateUtils.formatElapsedTime(it) }
    }
}